package net.cocotea.sqltoy.lambda.util;

import net.cocotea.sqltoy.lambda.common.SqlCharter;
import net.cocotea.sqltoy.lambda.common.SqlKeyword;

import java.util.List;
import java.util.StringJoiner;

/**
 * SQL片段工具类
 * 
 * @author CoCoTea
 * @since 0.0.3
 */
public class SqlFragmentUtils {

    private static String getMatchSql(String filedName, String valueName, String keyword) {
        StringJoiner fragmentJoiner = new StringJoiner(SqlCharter.SPACE);
        fragmentJoiner.add(filedName)
                .add(keyword)
                .add(SqlCharter.COLON + valueName);
        return fragmentJoiner.toString();
    }

    private static String getMatchSqlForList(String filedName, String valueName, String keyword) {
        StringJoiner fragmentJoiner = new StringJoiner(SqlCharter.SPACE);
        fragmentJoiner.add(filedName).add(keyword).add(valueName);
        return fragmentJoiner.toString();
    }

    public static String eq(String filedName, String valueName) {
        return getMatchSql(filedName, valueName, SqlKeyword.EQ);
    }

    public static String neq(String filedName, String valueName) {
        return getMatchSql(filedName, valueName, SqlKeyword.NEQ);
    }

    public static String gt(String filedName, String valueName) {
        return getMatchSql(filedName, valueName, SqlKeyword.GT);
    }

    public static String gte(String filed, String valueName) {
        return getMatchSql(filed, valueName, SqlKeyword.GTE);
    }

    public static String lt(String filed, String valueName) {
        return getMatchSql(filed, valueName, SqlKeyword.LT);
    }

    public static String lte(String filed, String valueName) {
        return getMatchSql(filed, valueName, SqlKeyword.LTE);
    }

    public static String like(String filed, String valueName) {
        return getMatchSql(filed, valueName, SqlKeyword.LIKE);
    }

    public static String notLike(String filed, String valueName) {
        return getMatchSql(filed, valueName, SqlKeyword.NOT_LIKE);
    }

    public static String leftLike(String filed, String valueName) {
        return getMatchSql(filed, valueName, SqlKeyword.LIKE);
    }

    public static String rightLike(String filed, String valueName) {
        return getMatchSql(filed, valueName, SqlKeyword.LIKE);
    }

    public static String in(String filed, String valueName) {
        return getMatchSqlForList(
                filed,
                PackUtils.parenthesis(SqlCharter.COLON + valueName),
                SqlKeyword.IN
        );
    }

    public static String notIn(String filed, String valueName) {
        return getMatchSqlForList(
                filed,
                PackUtils.parenthesis(SqlCharter.COLON + valueName),
                SqlKeyword.NOT_IN
        );
    }

    public static String between(String filed, Object value) {
        if (!(value instanceof List<?>)) {
            return null;
        }
        List<?> list = (List<?>) value;
        StringJoiner fragmentJoiner = new StringJoiner(SqlCharter.SPACE)
                .add(filed)
                .add(SqlKeyword.BETWEEN)
                .add(SqlCharter.COLON + list.get(0))
                .add(SqlKeyword.AND)
                .add(SqlCharter.COLON + list.get(1));
        return fragmentJoiner.toString();
    }
}
